package BuildData;

import java.io.BufferedWriter;
import java.io.IOException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Administrator
 */
public final class NumTreeData {

    private ArrayList itemAr = new ArrayList();
    private ArrayList tokenAr = new ArrayList();

    public NumTreeData() throws FileNotFoundException, XMLStreamException, IOException {

        System.out.println("package BuildData; class NumTreeData");

        setNswAr();
        setItemAr();
        writeToFile();
    }

    public void setNswAr() throws FileNotFoundException, XMLStreamException {
        FileInputStream fis = new FileInputStream("trainset\\hand\\num_hand.xml");
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String attributeValue = xmlStreamReader.getAttributeValue(0);

                        String nsw = xmlStreamReader.getElementText();


                        Token tkn = new Token();
                        tkn.setValue(nsw);
                        tkn.setNsw(attributeValue);
                        tokenAr.add(tkn);


                    } else {
                        if (localName.equals("URL")) {
                            String url = xmlStreamReader.getElementText();
                        }
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();

                    StringTokenizer st = new StringTokenizer(character);
                    while (st.hasMoreTokens()) {
                        String tokenStr = st.nextToken();
                        Token token = new Token();
                        token.setValue(tokenStr);
                        token.setNsw("NOT");
                        tokenAr.add(token);

                    }

            }
        }


    }

    public void setItemAr() {
        int ntim = 0;
        int ndat = 0;
        int nday = 0;
        int nmon = 0;
        int nnum = 0;
        int ntel = 0;
        int ndig = 0;
        int nscr = 0;
        int nrgn = 0;
        int nfrc = 0;
        int nadd = 0;
        int ncom = 0;
        ArrayList nswTrainSet = new ArrayList();
        for (int i = 0; i < tokenAr.size(); i++) {
            Token tkn = new Token();
            tkn = (Token) tokenAr.get(i);
            if (isNum(tkn.getValue())) {
                Num item = new Num();
                String tknValue = tkn.getValue();
                String tknNsw = tkn.getNsw();
                item.setClassNsw(tknNsw);
                item.setChaCount(tknValue.length());
                item.setNumCount(getNumCount(tknValue));
                item.setChamCount(getChamCount(tknValue));
                item.setCheoCount(getCheoCount(tknValue));
                item.setNgangCount(getNgangCount(tknValue));
                item.setHaiChamCount(getHaiChamCount(tknValue));
                item.setTknAf1(getToken(i + 1));
                item.setTknAf2(getToken(i + 2));
                item.setTknBf1(getToken(i - 1));
                item.setTknBf2(getToken(i - 2));
                item.setNswStr(tknValue);
                nswTrainSet.add(item);

                String classNsw = tkn.getNsw();
                if (classNsw.equals("NDAT")) {
                    ndat++;
                }
                if (classNsw.equals("NDAY")) {
                    nday++;
                }
                if (classNsw.equals("NMON")) {
                    nmon++;
                }
                if (classNsw.equals("NNUM")) {
                    nnum++;
                }
                if (classNsw.equals("NTEL")) {
                    ntel++;
                }
                if (classNsw.equals("NDIG")) {
                    ndig++;
                }
                if (classNsw.equals("NSCR")) {
                    nscr++;
                }
                if (classNsw.equals("NRGN")) {
                    nrgn++;
                }
                if (classNsw.equals("NADD")) {
                    nadd++;
                }
                if (classNsw.equals("NCOM")) {
                    ncom++;
                }
            }

        }

        System.out.println("    +nadd: " + nadd);
        System.out.println("    +ncom: " + ncom);
        System.out.println("    +ndat: " + ndat);

        System.out.println("    +nday: " + nday);
        System.out.println("    +ndig: " + ndig);
        System.out.println("    +nfrc: " + nfrc);
        System.out.println("    +nmon: " + nmon);
        System.out.println("    +nnum: " + nnum);
        System.out.println("    +nargn: " + nrgn);
        System.out.println("    +nscr: " + nscr);
        System.out.println("    +ntel: " + ntel);
        System.out.println("    +ntim: " + ntim);
        itemAr = nswTrainSet;


    }

    public void writeToFile() throws IOException {

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\tree\\numtreedata.db"), "UTF8"));
        bw.write("tree-database\n");
        bw.write("object name charCount numerical numCount numerical chamCount numerical cheoCount numerical ngangCount numerical haiChamCount numerical tknBf2 symbolic tknBf1 symbolic nswStr symbolic tknAf1 symbolic tknAf2 symbolic classNsw symbolic\n");

        ArrayList ar = new ArrayList();
        ar = getNswTrainSet();
        String item = "";
        for (int i = 0; i < ar.size(); i++) {
            item = "";
            Num nsw = (Num) ar.get(i);
            item += i;
            int charCount = nsw.getChaCount();
            item += " " + charCount;
            int numCount = nsw.getNumCount();
            item += " " + numCount;

            int chamCount = nsw.getChamCount();
            item += " " + chamCount;

            int cheoCount = nsw.getCheoCount();
            item += " " + cheoCount;

            int ngangCount = nsw.getNgangCount();
            item += " " + ngangCount;

            int haiChamCount = nsw.getHaiChamCount();
            item += " " + haiChamCount;

            String tknBf2 = nsw.getTknBf2();
            item += " " + tknBf2;
            String tknBf1 = nsw.getTknBf1();
            item += " " + tknBf1;
            String nswStr = nsw.getNswStr();
            item += " " + nswStr;
            String tknAf1 = nsw.getTknAf1();
            item += " " + tknAf1;
            String tknAf2 = nsw.getTknAf2();
            item += " " + tknAf2;

            String classNsw = nsw.getClassNsw();
            item += " " + classNsw;


            item += "\n";
            bw.write(item);

        }
        bw.close();

    }

    public ArrayList getNswTrainSet() {
        return itemAr;
    }

    public int getNumCount(String nsw) {
        int count = 0;
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                count++;
            }
        }
        return count;
    }

    public String getToken(int index) {
        if (index < 0 || index >= tokenAr.size()) {
            return "";
        } else {
            Token tkn = (Token) tokenAr.get(index);
            return tkn.getValue();
        }

    }

    public boolean isNum(String nsw) {
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public int getChamCount(String nsw) {
        Pattern p = Pattern.compile("\\.");
        Matcher m = p.matcher(nsw);
        int cham = 0;
        while (m.find()) {
            cham++;
        }
        return cham;
    }

    public int getNgangCount(String nsw) {
        Pattern p = Pattern.compile("\\p{Pd}");
        Matcher m = p.matcher(nsw);
        int ngang = 0;
        while (m.find()) {
            ngang++;
        }
        return ngang;
    }

    public int getCheoCount(String nsw) {
        Pattern p = Pattern.compile("/");
        Matcher m = p.matcher(nsw);
        int cheo = 0;
        while (m.find()) {
            cheo++;
        }
        return cheo;
    }

    public int getHaiChamCount(String nsw) {
        Pattern p = Pattern.compile(":");
        Matcher m = p.matcher(nsw);
        int haiCham = 0;
        while (m.find()) {
            haiCham++;
        }
        return haiCham;
    }
}
